package com.berchen.controller;

import com.berchen.common.result.Result;
import com.berchen.exception.ResponseException;
import com.berchen.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * params = {"!name","age","phone=!152"}
     * 表示 请求必须带上age和phone参数，并且phone!=152，同时不能带参数name否则都会报404的错误。
     * headers = {"token=123"}
     * 表示 请求头中必须有token并且值=123
     *
     * @return
     */
    @RequestMapping(value = "test",params = {"!name","age","phone=!152"},headers = {"token=123"})
    public Result test(){
        RedisTemplate redisTemplate = redisUtils.getRedisTemplate();

        redisUtils.lPushAll("list1",Arrays.asList(1,2,3,4,5,5,6,7,90));
        return Result.ok();
    }

    /**
     * HttpEntity<String> 包含请求头
     * RequestEntity<String> 比上面的多一个请求路径
     * ResponseEntity<String>
     * @param str
     * @param requestEntity
     * @return
     */
    @RequestMapping("test1")
    public ResponseEntity<String> test1(HttpEntity<String> str, RequestEntity<String> requestEntity){

        System.out.println(str);
        System.out.println(requestEntity);
        return new ResponseEntity<>("ResponseEntity", HttpStatus.OK);
    }

//    @ResponseStatus(reason = "系统异常",value = HttpStatus.BAD_REQUEST)   // 该注解和拦截器有冲突
    @RequestMapping("exception")
    public String exception(int age){

        try {
            if (age == 1) {
                throw new ResponseException();
            }
            return "success";
        }catch (Exception e){

            return "error";
        }
    }
}
